package  
{
	import events.LoaderEvent;
	import flash.display.DisplayObject;
	import flash.display.DisplayObjectContainer;
	import flash.display.Loader;
	import flash.display.LoaderInfo;
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.ProgressEvent;
	import flash.net.URLRequest;
	import flash.text.Font;
	/**
	 * ...
	 * @author Kasoi
	 */
	public class Lib extends EventDispatcher
	{
		[Event(name = "loadComplete", type = "events.LoaderEvent")]
		[Event(name = "progress", type = "events.LoaderEvent")]
		
		[Embed(source = '../public/preLib.swf', symbol = 'prelib_test_item')]
		static private var prelib_test_itemClass: Class;
		
		static public function get prelib_test_item() : DisplayObject
		{
			return (new Lib.prelib_test_itemClass() as DisplayObject);
		}
		
		
		////////////////////////////////////////////////////////////////////
		// Lazy singleton and loading lib
		////////////////////////////////////////////////////////////////////
		
		static private var _instance: Lib;
		
		static public function get instance() : Lib
		{
			return Lib._instance;
		}
		
		public function Lib() 
		{
			
		}
		
		static private var isInitialized: Boolean = false;
		
		static public function init() : void 
		{
			if (Lib.isInitialized) throw new Error('Lib is already initialized');
			Lib.isInitialized = true;
			Lib._instance = new Lib();
		}
		
		////////////////////////////////////////////////////////////////////
		// Private properties
		////////////////////////////////////////////////////////////////////
		
		private var isLoading: Boolean = false;
		
		private var loader: Loader;
		
		////////////////////////////////////////////////////////////////////
		// Public methods
		////////////////////////////////////////////////////////////////////
		
		public function loadLibraries() : void 
		{
			if (this.isLoading) throw new Error('Libraries already loading');
			this.isLoading = true;
			
			this.loader = new Loader();
			this.loader.load(new URLRequest(Settings.LIB_PATH));
			this.loader.contentLoaderInfo.addEventListener(Event.COMPLETE, 
				this.libLoader_onLoad);
			this.loader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, 
				this.libLoader_onProgress);
		}
		
		////////////////////////////////////////////////////////////////////
		// Listeners
		////////////////////////////////////////////////////////////////////
		
		private function libLoader_onLoad(e: Event) : void 
		{
			this.dispatchEvent(new LoaderEvent(LoaderEvent.LOAD_COMPLETE));
		}
		
		private function libLoader_onProgress(e: Event) : void 
		{
			var loaded: Number = this.loader.contentLoaderInfo.bytesLoaded;
			var total: Number = this.loader.contentLoaderInfo.bytesTotal;
			var progress: Number = loaded / total;
			this.dispatchEvent(new LoaderEvent(
				LoaderEvent.PROGRESS, progress));
		}
		
		private function getObject(name: String) : Class
		{
			var info: LoaderInfo = this.loader.contentLoaderInfo;
			return info.applicationDomain.getDefinition(name) as Class;
		}
		
		////////////////////////////////////////////////////////////////////
		// Lib Data
		////////////////////////////////////////////////////////////////////
		
		static public function get lib_test_item() : DisplayObject
		{
			var cl: Class = Lib.instance.getObject('lib_test_item');
			return (new cl()) as DisplayObject;
		}
		
	}

}